package org.example.jedis.zset;

import org.example.jedis.JedisDemo;
import org.junit.Test;
import redis.clients.jedis.Tuple;

import java.util.HashMap;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/25  16:21
 */
public class ZSetControlTest extends JedisDemo {


    /**
     * 命令:zadd key score1 value1 score2 value2 ...
     * 将一个或多个member元素及其score值加入到key对应的有序集合中。
     */
    @Test
    public void zaddKeyScoresValues(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);
    }


    /**
     * 命令:zrange key startindex endindex
     * 获取key对应的有序集合元素索引在startindex到endindex的元素，获取到的结果是按评分从小到大的。
     */
    @Test
    public void zrangeKeyStartIndexEndIndex(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        long startIndex = 0;
        long endIndex = -1;
        for (String s : jedis.zrange(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{}",key1,s);
        }
        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }
    }


    /**
     * 命令:zrangebyscore key min max
     * 获取key对应有序集合中评分大于等于min和小于等于max的元素；并且按评分从小到大排序
     */
    @Test
    public void zrangeByScoreKeyMinMax(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        double min = 100;
        double max = 200;
        for (String s : jedis.zrangeByScore(key1, min, max)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{}",key1,s);
        }
        for (Tuple zrangeWithScore : jedis.zrangeByScoreWithScores(key1, min, max)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }
    }


    /**
     * 命令:zrevrangebyscore key max min
     * 获取key对应有序集合评分在小于等于max和大于等于min的范围内的元素，并按评分从大到小排序
     */
    @Test
    public void zreverangebyscoreKeyMaxMin(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        double min = 100;
        double max = 200;
        for (String s : jedis.zrevrangeByScore(key1, max, min)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{}",key1,s);
        }
        for (Tuple zrangeWithScore : jedis.zrevrangeByScoreWithScores(key1, max, min)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }
    }


    /**
     * 命令:zincrby key increment member
     * 为key对应的有序集合中的member元素增加increment
     */
    @Test
    public void zincrbyKeyIncrementMember(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        long startIndex = 0;
        long endIndex = -1;
        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }

        double increment = 500;
        Double zincrby = jedis.zincrby(key1, increment, member3);
        LOGGER.info("zincrby执行的key:{}的结果为:{}",key1,zincrby);

        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }
    }


    /**
     * 命令:zrem key member
     * 在key对应的有序集合中，删除member元素
     */
    @Test
    public void zremKeyMember(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        long startIndex = 0;
        long endIndex = -1;
        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }

        Long zrem = jedis.zrem(key1, member1);
        LOGGER.info("zrem执行的key:{}的结果为:{}",key1,zrem);

        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }
    }


    /**
     * 命令:zcount key min max
     * 统计key对应有序集合中评分在min到max间的元素个数
     */
    @Test
    public void zcountKeyMinMax(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        long startIndex = 0;
        long endIndex = -1;
        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }

        double min = 100;
        double max = 200;
        Long zcount = jedis.zcount(key1, min, max);
        LOGGER.info("统计key:{}对应有序集合中评分在{}到{}范围的元素个数,结果为:{}",key1,min,max,zcount);
    }


    /**
     * 命令:zrank key member
     * 返回该值在集合中的排名，从0开始。
     */
    @Test
    public void zrankKeyMember(){
        String key1 ="k1";
        String member1 = "SqlLite";
        String member2 = "Mysql";
        String member3 = "Redis";

        double score1 = 100;
        double score2 = 200;
        double score3 = 300;
        delExistsKeyValue(key1);
        HashMap<String, Double> map = new HashMap<>();
        map.put(member1,score1);
        map.put(member2,score2);
        map.put(member3,score3);
        Long zadd = jedis.zadd(key1, map);
        LOGGER.info("保存key:{}对应的有序集合,结果为:{}",key1,zadd);

        long startIndex = 0;
        long endIndex = -1;
        for (Tuple zrangeWithScore : jedis.zrangeWithScores(key1, startIndex, endIndex)) {
            LOGGER.info("key:{}对应的有序集合中元素为:{},评分为:{}",key1,zrangeWithScore.getElement(),zrangeWithScore.getScore());
        }

        Long zrank = jedis.zrank(key1, member3);
        LOGGER.info("key:{}对应的有序集合中元素:{}的排名为:{}",key1,member3,zrank);
    }
}
